<?php
namespace Libs\Common;
/**
 * libs function common
 * @author DucLH
 * */
class LibFunctionCommon {
	/**
	 *
	 * @author DucLH
	 * */
	public static function getCommonErr($aErr, $sListStt) {
		//
		if ($sListStt == "" || count($aErr) == 0)
			return array();
		//
		$aReturn = array();
		$aListStt = explode("-", substr($sListStt, 0, -1));
		foreach ($aListStt as $key => $value) {
			//var_dump("<pre>", $key, $key,$aListStt);die();
			if (isset($aErr[$value])) {
				$aReturn[$value] = $aErr[$value];
				break;
			}
		}
		//var_dump("<pre>",$aErr, $sListStt, $aListStt, $aReturn);
		return $aReturn;
	}

	/*
	 *   @Function Name  :   enCodeForApi
	 *   @Description    :   encode with key
	 *   @Author         :   DucLH
	 *   @Input          :   string
	 *   @photoname      : string (example: sp_larepphoto_1.jpg or sp_smallphoto_1.jpg)
	 *
	 *   @Output         :   return strign encoded
	 */
	public static function enCodeForAll($id, $photoname = '') {
		$contant = 'aishow_mykey';

		$id = base64_encode($photoname) . base64_encode($contant) . base64_encode($id);
		return $id;

	}

	/*
	 *   @Function Name  :   deCodeForApi
	 *   @Description    :   deCodeForApi with key
	 *   @Author         :   DucLH
	 *   @Input          :   string
	 *
	 *   @Output         :   return strign decoded
	 */
	public static function deCodeForAll($id) {
		$contant = 'aishow_mykey';
		$unhashcontant = base64_encode($contant);
		$explode = explode($unhashcontant, $id, 2);
		if (count($explode) == 2) {
			if (!empty($explode[0])) {
				$id = base64_decode($explode[1]) . '/' . base64_decode($explode[0]);
			} else {
				$id = base64_decode($explode[1]);
			}

		}
		return $id;
		/*$strlen = strlen(base64_encode($contant));
		 $id = base64_decode(substr($id, $strlen));
		 return $id;*/

	}

	/**
	 * Get the number of characters no matter what encoding. (it should be "getStringLength")
	 * @author DucLH
	 * @param string
	 * @return int
	 */
	static function getStringLenght($str) {
		$num = 1;
		$arr = array();
		$x = floor(mb_strlen($str, "UTF-8") / $num);

		$i = 0;
		$j = 0;
		while ($i <= $x) {
			$y = mb_substr($str, $j, $num, "UTF-8");
			if ($y != "") {
				array_push($arr, $y);
			}
			++$i;
			$j = $j + $num;
		}
		return count($arr);
	}

	/**
	 * author: DucLH
	 * @param $str
	 * Description: check number halfsize
	 * @return
	 */
	static function isNumberHalfSize($thetext) {
		if (strlen($thetext) != mb_strlen($thetext, "UTF-8")) {
			return false;
		}
		if (!is_numeric($thetext)) {
			return false;
		}
		return true;
	}

	/**
	 * author: Duclh
	 * @param $str
	 * Description: split $thetext
	 * @return
	 */
	static function strsplt($thetext, $num = 1) {
		//$thetext = mb_convert_encoding($thetext,"UTF-8","shift_jis");
		if (!$num) {
			$num = 1;
		}
		$arr = array();
		$x = floor(mb_strlen($thetext, "UTF-8") / $num);
		$i = 0;
		$j = 0;
		while ($i <= $x) {
			$y = mb_substr($thetext, $j, $num, "UTF-8");
			if ($y) {
				array_push($arr, $y);
			}
			$i++;
			$j = $j + $num;
		}
		return $arr;
	}

	/**
	 * Generate random string.
	 * @author DucLh
	 * @param int Length
	 * @return string
	 */
	public static function generateRandomString($length = 10) {
		$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
		$randomString = '';
		for ($i = 0; $i < $length; $i++) {
			$randomString .= $characters[rand(0, strlen($characters) - 1)];
		}
		return $randomString;
	}

	/**
	 * decode base 64 for qmail subject
	 */
	public static function decodeBase64ForQmailSubject($sSubject) {
		if (strpos($sSubject, "?UTF-8?B?") !== false) {
			$sSubject = str_replace("?UTF-8?B?", "", $sSubject);
			$sSubject = base64_decode($sSubject);
		}
		return $sSubject;
	}

	/**
	 * Get email not format <asasd> email
	 * 
	 * @author TinNT1
	 */
	public static function getEmailNotFormat($sMailAddress) {
		if (strpos($sMailAddress, "<") !== false) {
			$pattern = '/[<>]/';
			$sMailAddress = preg_split($pattern, $sMailAddress);
			$sMailAddress = $sMailAddress[1];
		}
		return $sMailAddress;
	}

}
